﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page 
{
    int page_current = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1)
            Response.Redirect("../login.aspx");

        if (((int)Session["permission"]) != 2 && ((int)Session["permission"]) != 3)
        {
            menu.Visible = false;
            menu.Enabled = false;
        }

        // get current page
        if (Request.QueryString["page"] != null)
        {
            try
            {
                page_current = Int32.Parse(Request.QueryString["page"]);
            }
            catch 
            {
                page_current = 1;
            }
        }
        
        // bind data
        if (!IsPostBack)
        {
            string strOrderby =orderby.SelectedValue;
            if (Session["orderby"] != null)
            {
                strOrderby = Session["orderby"].ToString();
                orderby.Items.FindByValue(orderby.SelectedValue).Selected = false;
                orderby.Items.FindByValue(strOrderby).Selected = true;
            }
            string strOrder = order.SelectedValue;
            if (Session["order"] != null)
            {
                strOrder = Session["order"].ToString();
                order.Items.FindByValue(order.SelectedValue).Selected = false;
                order.Items.FindByValue(strOrder).Selected = true;
            }
            BindData(strOrderby, strOrder);

            // load list priority
            priority_project.Attributes["style"] = "margin:0;";
            ListItem liItem = new ListItem(DisplayEnum.DisplayPriority((int)Priority.normal), ((int)Priority.normal).ToString());
            liItem.Selected = true;
            priority_project.Items.Add(liItem);

            liItem = new ListItem(DisplayEnum.DisplayPriority((int)Priority.priority), ((int)Priority.priority).ToString());
            priority_project.Items.Add(liItem);
            
            liItem = new ListItem(DisplayEnum.DisplayPriority((int)Priority.hurry), ((int)Priority.hurry).ToString());
            priority_project.Items.Add(liItem);

            liItem = new ListItem(DisplayEnum.DisplayPriority((int)Priority.urgent), ((int)Priority.urgent).ToString());
            priority_project.Items.Add(liItem);       
        }
    }

    private void BindData(string orderby,string order)
    {
        //display.Text = orderby + " " + order;
        int page_number = 0;

        // load list of project
        if (Session["id_user"] != null && Session["permission"] != null)
        {
            SqlParameter[] listParams = new SqlParameter[6];
            //-- param 1: @id_user
            int id = Int32.Parse(Session["id_user"].ToString());
            SqlParameter param = new SqlParameter("@id_user", id);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[0] = param;

            //-- param 2: @page_size
            param = new SqlParameter("@page_size", 10);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[1] = param;

            //-- param 3: @page_current
            param = new SqlParameter("@page_current", page_current);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[2] = param;
    
            // -- param 4: @orderby
            param = new SqlParameter("@orderby",orderby);
            param.SqlDbType = SqlDbType.VarChar;
            param.Size = 50;
            param.Direction = ParameterDirection.Input;
            listParams[3] = param;

            // -- param 5: @typeorder
            param = new SqlParameter("@typeorder",order);
            param.SqlDbType = SqlDbType.VarChar;
            param.Size = 50;
            param.Direction = ParameterDirection.Input;
            listParams[4] = param;

            //-- param 6: @page_number
            param = new SqlParameter();
            param.ParameterName = "@page_number";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Output;
            listParams[5] = param;
        
            DataTable dtTable = dal.SelectTableSP("proUser_Project_SEL", listParams);
            page_number = Int32.Parse(listParams[5].Value.ToString());

            // display full info of projects in gridview
          
            projects.DataSource = dtTable;
            projects.DataBind();
            int permisssion=(int)Session["permission"];
            if (permisssion != 2 && permisssion != 3)
                projects.Columns[8].Visible = false;
        }

        // display pager
        if (page_number == 0)
        {
            display.Text = "Chưa có dự án nào trong hệ thống";
            pager.Visible = false;
            this.order.Visible = false;
            this.orderby.Visible = false;
            this.label1.Visible = false;
            this.label2.Visible = false;
        }
        else if (page_number == 1)
        {
            pager.Visible = false;
            display.Text = "";
        }
        else if (page_number > 1)
        {
            display.Text = "";
            // load pager
            pager.link = "~/home/default.aspx";
            pager.numberpage = page_number;
            pager.currentpage = page_current;
            pager.paramPageName = "page";
            pager.Visible = true;
            pager.BindData();
        }
    }

    protected void btOk_click(object sender, EventArgs e)
    {
        string strOrderby = (Session["orderby"] != null ? Session["orderby"].ToString() : orderby.SelectedValue);
        string strOrder = (Session["order"] != null ? Session["order"].ToString() : order.SelectedValue);
        BindData(strOrderby, strOrder);
        header.BindData();
    }
    
    protected void projects_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "removeproject")
        {
            //string sql = "DELETE FROM project WHERE id="+comm.to_sql(e.CommandArgument.ToString(),"number");
            //dal.Execute(sql);
            string strOrderby = (Session["orderby"] != null ? Session["orderby"].ToString() : orderby.SelectedValue);
            string strOrder = (Session["order"] != null ? Session["order"].ToString() : order.SelectedValue);
            BindData(strOrderby, strOrder);
            header.BindData();
        }
    }

    //public string SelectStyleForPriority(object value)
    //{
    //    try
    //    {
    //        int pri = Int32.Parse( value.ToString());
    //        switch (pri)
    //        {
    //            case 2:
    //                break;
    //            case 3:
    //                return "priority_pri";
    //                break;
    //            case 4:
    //                return "hurry_pri";
    //                break;
    //            case 5:
    //                return "urgent_pri";
    //                break;
    //            default:
    //                return "normal_pri";
    //        }
    //    }
    //    catch
    //    { 
    //        return "normal_pri";
    //    }

    //    return "normal_pri";
    //}

    protected void orderby_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strOrderby = ((DropDownList)sender).SelectedValue;
        Session.Add("orderby",strOrderby);
        Session.Timeout = 60;
        string strOrder = order.SelectedValue;
        BindData(strOrderby,strOrder);
    }
    protected void order_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strOrder = ((DropDownList)sender).SelectedValue;
        Session.Add("order",strOrder);
        Session.Timeout = 60;
        string strOrderby = orderby.SelectedValue;
        BindData(strOrderby, strOrder);
    }
}
